package com.wisewin.pose.service;

import com.wisewin.pose.config.dto.ResultDTO;
import com.wisewin.pose.config.dto.ResultDTOBuilder;
import com.wisewin.pose.dao.OrgUserDAO;
import com.wisewin.pose.dao.UserOrgAuthorizationDAO;
import com.wisewin.pose.entity.bo.QueryParamBO;
import com.wisewin.pose.entity.db.OrgUserDO;
import com.wisewin.pose.util.PageInfo;
import com.wisewin.pose.util.PageObjectUtil;
import com.wisewin.pose.util.PageUtil;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class OrgUserService {

    @Autowired
    private OrgUserDAO orgUserDAO;
    @Autowired
    private UserOrgAuthorizationDAO userOrgAuthorizationDAO;

    public OrgUserDO queryOrgUserForLogin(String userName, String passcode) {
        return orgUserDAO.queryOrgUserForLogin(userName, passcode);
    }

    public OrgUserDO queryById(Integer id) {
        return orgUserDAO.queryById(id);
    }

    /**
     * 获取用户列表
     */
    public ResultDTO selectAuthoredUsersByOrgId(QueryParamBO queryParamBO, PageUtil pageUtil) {
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", queryParamBO.getOrgId());
        map.put("userName", queryParamBO.getUserName());
        map.put("startTime", queryParamBO.getStartTime());
        map.put("endTime", queryParamBO.getEndTime());
        map.put("phone", queryParamBO.getPhone());
        map.put("sex", queryParamBO.getSex());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());

        PageObjectUtil page = new PageObjectUtil();
        @SuppressWarnings("rawtypes")
        PageInfo pageData = page.savePageObject(userOrgAuthorizationDAO.queryAuthoredUserCount(map),
                userOrgAuthorizationDAO.selectAuthoredUsersByOrgId(map),
                pageUtil);
        return ResultDTOBuilder.success(pageData);
    }

    public int insertOrIgnore(String openId, Integer orgId) {

        return userOrgAuthorizationDAO.insertOrIgnore(openId, orgId);
    }
}
